package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * %test%控制器
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }


    //编辑
    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:bpmnInfo:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id){
       bpmnInfoService.delete(id);
          return AjaxResult.success();
        }

    //流程文件部署页面跳转
    @RequiresPermissions("business:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    public String deploy(){
        return prefix + "deploy";
    }

    //流程文件上传
    @RequiresPermissions("business:bpmnInfo:uploadfiles")
    @RequestMapping("/uploadfiles")
    @ResponseBody
    public AjaxResult uploadfiles(MultipartFile file){
        String uploadFiles = bpmnInfoService.uploadFiles(file);
        return AjaxResult.success("文件上传成功",uploadFiles);
    }

    //流程定义部署
    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String bpmnInfoFile,String bpmnType,String info ){
        bpmnInfoService.save(bpmnInfoFile,bpmnType,info);
        return AjaxResult.success();
    }

    //文件查询
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    public void readResource(String deploymentId,String type ,HttpServletResponse Response) throws IOException {
        IOUtils.copy(bpmnInfoService.readResource(deploymentId,type),Response.getOutputStream());

    }

    //部署备注信息保存
    @RequiresPermissions("business:bpmnInfo:updataInfo")
    @RequestMapping("/updataInfo")
    @ResponseBody
    public AjaxResult updataInfo(Long id,String info) {
        bpmnInfoService.updataInfo(id,info);
        return AjaxResult.success();
    }


}
